package main

import (
	"fmt"
	"notes/config"
	"notes/controller"
	_ "notes/docs"
	"notes/middleware"

	"github.com/fvbock/endless"
	"github.com/gin-contrib/cors"
	"github.com/gin-contrib/static"
	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// @title 个人笔记简易网站
// @version 1.0
// @description gin-web框架
// @contact.name  jone
// @contact.email jonetwelve@qq.com
// @host 127.0.0.1:50000
// @basepath /api
func main() {
	gin.DisableConsoleColor()
	if config.Conf.Debug {
		gin.SetMode(gin.DebugMode)
	} else {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	r.Use(gin.Recovery())
	if config.Conf.Debug {
		r.Use(cors.Default())
		fmt.Printf("debug on----- \n")
	}
	r.Static("/backstage", "./manager_dist")
	r.Use(static.Serve("/", static.LocalFile("./front_dist", false)))

	// 添加文档
	if config.Conf.Debug {
		r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
	}

	api := r.Group("/api")
	manager := api.Group("")
	manager.Use(middleware.CheckToken)

	{ // 后台接口
		manager.POST("/category/add", controller.AddCategory)
		manager.GET("/category/delete", controller.DeleteCategory)

		manager.POST("/article/add", controller.AddArticle)
		manager.POST("/article/update", controller.UpdateArticle)
		manager.GET("/article/delete", controller.DeleteArticle)
		manager.GET("/article/change-category", controller.ChangeArticleCategory)

		manager.POST("/tag/add", controller.AddTag)
		manager.GET("/tag/delete", controller.DeleteTag)
	}

	{
		api.POST("/admin/login", controller.Login)

		api.GET("/category/get", controller.GetCategory)

		api.GET("/article/get", controller.GetArticleByCid)
		api.GET("/article/search", controller.SearchArticle)
		api.GET("/article/find", controller.FindArticle)

		api.GET("/tag/get", controller.GetTag)
		api.GET("/tag/get-articles", controller.GetTagArticles)
	}

	fmt.Printf("start server on: %s\n", config.Conf.Server.Address)
	s := endless.NewServer(config.Conf.Server.Address, r)
	s.ListenAndServe()
}
